package com.llyb.chap02;

/**
 *
 * 死锁案例
 */
/*
public class DeadLockDemo {


    public static void main(String[] args) {

        Object A = new Object();
        Object B = new Object();

        new Thread(()->{
            try {
                System.out.println("A开始强A锁");
                synchronized (A){
                    System.out.println("A抢到了A锁");
                    Thread.sleep(300);
                    System.out.println("A开始强B锁");
                    synchronized (B){
                        System.out.println("A抢到了B锁");
                        Thread.sleep(300);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(()->{
            try {
                System.out.println("B开始强B锁");
                synchronized (B){
                    System.out.println("B抢到了B锁");
                    Thread.sleep(300);
                    System.out.println("B开始强A锁");
                    synchronized (A){
                        System.out.println("B抢到了A锁");
                        Thread.sleep(300);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();
    }
}
*/

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 死锁的解决
 */
public class DeadLockDemo {


    public static void main(String[] args) {

        ReentrantLock A = new ReentrantLock();
        ReentrantLock B = new ReentrantLock();

        new Thread(()->{
            try {
                System.out.println("A开始强A锁");
                boolean flag1 = A.tryLock();

                if (flag1) {
                    System.out.println("A抢到了A锁");
                    Thread.sleep(300);
                    System.out.println("A开始强B锁");
                    boolean flag2 = B.tryLock(100, TimeUnit.MILLISECONDS);
                    if (flag2) {
                        System.out.println("A抢到了B锁");
                        Thread.sleep(300);
                        System.out.println(Thread.currentThread().getName()+"\t"+"---- 执行结束");
                        B.unlock();
                        System.out.println("B释放了B锁");
                        A.unlock();
                        System.out.println("B释放了A锁");
                    }else {
                        A.unlock();
                        System.out.println("A没抢到B锁");
                    }
                }else {
                    System.out.println("A没抢到A锁");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(()->{
            try {
                System.out.println("B开始强B锁");
                boolean flag1 = B.tryLock(200, TimeUnit.MILLISECONDS);

                if (flag1) {
                    System.out.println("B抢到了B锁");
                    Thread.sleep(300);
                    System.out.println("B开始强A锁");
                    boolean flag2 = A.tryLock(500, TimeUnit.MILLISECONDS);
                    if (flag2) {
                        System.out.println("B抢到了A锁");
                        Thread.sleep(300);
                        System.out.println(Thread.currentThread().getName()+"\t"+"---- 执行结束");
                        B.unlock();
                        System.out.println("B释放了B锁");
                        A.unlock();
                        System.out.println("B释放了A锁");
                    }else {
                        B.unlock();
                        System.out.println("B释放了B锁");
                        System.out.println("B没抢到A锁");
                    }
                }else {
                    System.out.println("B没抢到B锁");
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();
    }
}


